package com.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

/**
 * @author linyu.dai
 * @project rpc
 * @description
 * @creat 2022/11/27 18:48:34
 */
@Slf4j
public class ClassScan {

    /**
     * @return A
     * @Author linyu.dai
     * @Description 这个类上的某个注解
     * @Date 2022/11/27 20:35
     * @Param [path, annotationClass]
     */
    public static <A> A getAnnotation(String path, Class<? extends Annotation> annotationClass) {
        A annotation = null;
        try {
            Class<?> aClass = Class.forName(path);
            if(aClass.isAnnotationPresent(annotationClass)){
                return (A)aClass.getAnnotation(annotationClass);
            }
        } catch (ClassNotFoundException e) {
           log.error("类加载失败 ...{}",e);
        }
        return (A)annotation;
    }

    /**
     * @Author linyu.dai
     * @Description 扫描所有的类
     * @Date 2022/11/27 20:31
     * @Param [path]
     * @return java.util.List<java.lang.String>
     */
    public static List<String> scan(String path){
        String replacePath = path.replace(".","/");
        ArrayList<String> classPaths = new ArrayList<>();
        String file = Objects.requireNonNull(ClassScan.class.getClassLoader().getResource(replacePath)).getFile();
        Path path1 = new File(file).toPath();

        try {
            Files.walkFileTree(path1, new SimpleFileVisitor<>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    //是class文件
                    if(file.getFileName().toString().endsWith(".class")){
                        String s = file.toString().replace("\\",".");
                        String classPath = s.substring(s.lastIndexOf(path), s.lastIndexOf(".class"));
                        classPaths.add(classPath);
                    }
                    return super.visitFile(file, attrs);
                }
            });
        } catch (IOException e) {
            log.error("file scan error:{}",e.getMessage());
        }
        return classPaths;
    }

//    public static void main(String[] args) throws IOException {
//        String file = Objects.requireNonNull(ClassScan.class.getClassLoader().getResource(SCAN)).getFile();
//        //通过文件去获取path对象
//        Path path1 = new File(file).toPath();
//        //获取所有文件 包括目录和文件
////        Files.walk(path1).forEach(System.out::println);
//        Files.walkFileTree(path1, new SimpleFileVisitor<>(){
//
//            //遍历文件进行处理
//            @Override
//            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
//                String s = file.toString();
//                String substring = s.substring(s.lastIndexOf(SCAN), s.lastIndexOf(".class")).replace("\\",".");
//                System.out.println(substring);
//                return super.visitFile(file, attrs);
//            }
//
//        });
    }
